<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<?php 
//class start

class User
{
public function connection(){
		$myhost="localhost";
		$myuser="root";
		$mypass="";
		$mydb="elearning";	
	     /* Connecting, selecting database */
	     mysql_connect($myhost, $myuser, $mypass)
		 or die("Could not connect : " . mysql_error());
	     /*print "Connected successfully";*/
	     mysql_select_db($mydb) or die("Could not select database");
		}

public function registration_user($fname, $mname, $lname, $gender,
								  $birthdate, $address, $cp, $phone, 
								  $email, $username, $password)
//start reg_user
{								  
//variable that holds password for encrption 
$password = md5($password);
//query select checks if email and username are already exist
$query = mysql_query("SELECT userid from tblUser WHERE username = '$username'
					 or email = '$email'"); 

//if no rows no found 
$no_rows = mysql_num_rows($query);

//if no user will register 
if ($no_rows == 0):

		
		$result = mysql_query("INSERT INTO tblUser (fname, mname, lname, gender, 
							  birthdate, address, cp, phone, email, username, password) 
							  values ('$fname' , '$mname' , '$lname' , '$gender',
							  '$birthdate', '$address', '$cp', '$phone', '$email',
							  '$username' , 'password')") or die (mysql_error());
		return $result;
	
else:
		return FALSE;
		
endif;
	
} //close reg_user


//user id start

public function showUser(){
			$user_id = $_SESSION['curuser'];
			$user_type = $_SESSION['user_type'];
			$useruname = $_SESSION['us'];
			$user = mysql_query("SELECT *, CONCAT(SUBSTR(mname,1,1)) as m FROM tblUser WHERE userid = '$user_id'");
			$row_user= mysql_fetch_assoc($user); 
			
			echo "<span> [".$row_user['user_type'] ."] - " . $row_user['fname'] ." ".$row_user['m'].". ". $row_user['lname'] . "</span>";
			
		}
public function titleUser(){
		$user_id = $_SESSION['curuser'];
			$user_type = $_SESSION['user_type'];
			$useruname = $_SESSION['us'];
			$user = mysql_query("SELECT *, CONCAT(SUBSTR(mname,1,1)) as m FROM tblUser WHERE userid = '$user_id'");
			$row_user= mysql_fetch_assoc($user); 
			
			echo "" . $row_user['fname'] ." ".$row_user['m'].". ". $row_user['lname'];
			
}
public function oras(){
	date_default_timezone_set('Asia/Taipei');
		$date = date('l, M jS, Y (H:i:s a)');
				echo "Today is: " .$date;	
}
public function showPic(){
			$user_id = $_SESSION['curuser'];
			$user = mysql_query("SELECT * FROM tblUser WHERE userid = '$user_id'");
			$row_pic= mysql_fetch_assoc($user); 
			  echo "<img src=\"../view/avatar/".$row_pic['avatar']."\" width=\"50\" height=\"50\"  /><br><br>"; 
	
			
		}
public function viewSender(){
			$user_id = $_SESSION['curuser'];
			$user_type = $_SESSION['user_type'];
			$useruname = $_SESSION['us'];
			$user = mysql_query("SELECT * FROM tblUser WHERE userid = '$user_id'");
			$row_user= mysql_fetch_assoc($user); 
			echo "<span>" . $row_user['fname'] ." ". $row_user['lname'] . "</span>";
			
		}

//close id start

//view students

public function view_students(){
	print '
	<table width="680" align="center" bordercolor="#000000" " bgcolor="#ebf9b4" >
	<tr>
		<td align="left" colspan="6"><b>Enrolled List</b></td>
    </tr>
        <tr>
			<td class="label" align="center"><strong>No.</strong></td>
            <td class="label" align="center"><strong>Student Name</strong></td>
            <td class="label" align="center"><strong>Section</strong></td>
           <td class="label" align="center"><strong>Date and Time</strong></td>
	
	';
		$section = mysql_query("SELECT * FROM tblUser u INNER JOIN tblsection s ON s.studentNumber = u.userid WHERE user_type = 'student' AND forumaccess = 1 AND status = 'yes' ");
		$noOfSection = mysql_num_rows($section);
		$loop = 1;
		$ctr = 1;
		
		while($ctr <= $noOfSection){
		
		while($row_section = mysql_fetch_assoc($section)):
			$u = $row_section['userid'];
			date_default_timezone_set('Asia/Manila');
			$date = date('F j, Y, g:i a', strtotime($row_section['datejoin']));
		
		$a++;
		if ($a%2==1)
		{ 
		echo "<tr bgcolor='#e6e4b1'>";
		}
		else
		{
			echo "<tr bgcolor='#c9e362'>";	
		}
		
		print '
						<td align="left">'.$loop.'.</td>
						<td align="center" class="extra"><a href="viewUsers.php?id='.$u.'">'.$row_section['fname'].' '.$row_section['mname'].' '.$row_section['lname'].'</a></td>
						<td align="center" class="extra">'.$row_section['sectionName'].'</td>
						<td align="center" class="extra">'.$date.'</td>
					
						';	
		$loop++;
		endwhile;
		$ctr++;
		}
 print '</table>';
}


//check
function StartValidate() {
	
	// Assign some var's for the requests
	$required = $_GET["required"];
	$type = $_GET["type"];
	$value = $_GET["value"];

	// This is the function to check if a field is even required or not
	// So it's useful if you only want to check if it isn't empty
	validateRequired($required, $value, $type);

	switch ($type) {
		case 'number':
			validateNumber($value);
			break;
		case 'alphanum':
			validateAlphanum($value);
			break;
		case 'alpha':
			validateAlpha($value);
			break;
		case 'date':
			validateDate($value);
			break;
		case 'email':
			validateEmail($value);
			break;
		case 'url':
			validateUrl($value);
			break;
	}
}

// The function to check if a field is required or not
function validateRequired($required, $value, $type) {
	if($required == "required") {

		// Check if we got an empty value
		if($value == "") {
			echo "false";
			exit();
		}
	} else {
		if($value == "") {
			echo "none";
			exit();
		}
	}
}

// I use regular expressions in order to check a field's input, you can
// get most of them at the Regex Library at http://www.regexlib.com
// There you can check your own regular expressions, too

// Validation of an Email Address
function validateEmail($value) {
	if(ereg("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", $value, $regs)) {
		echo "true";
	} else {
		echo "false";
	}
}

// Validation of a date
function validateDate($value) {
	if(ereg("^(([1-9])|(0[1-9])|(1[0-2]))\/(([0-9])|([0-2][0-9])|(3[0-1]))\/(([0-9][0-9])|([1-2][0,9][0-9][0-9]))$", $value, $regs)) {
		echo "true";
	} else {
		echo "false";
	}
}

// Validation of an URL
function validateUrl($value) {
	if(ereg("^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*[^\.\,\)\(\s]$", $value, $regs)) {
		echo "true";
	} else {
		echo "false";
	}
}

// Validation of characters
function validateAlpha($value) {
	if(ereg("^[a-zA-Z]+$", $value, $regs)) {
		echo "true";
	} else {
		echo "false";
	}
}

// Validation of characters and numbers
function validateAlphanum($value) {
	if(ereg("^[a-zA-Z0-9]+$", $value, $regs)) {
		echo "true";
	} else {
		echo "false";
	}
}

// Validation of numbers
function validateNumber($value) {
	if(ereg("^[0-9]+$", $value, $regs)) {
		echo "true";
	} else {
		echo "false";
	}
}


//not entrolled

public function view_not_enroll(){
		print '
	<table width="680" align="center" bordercolor="#000000" " bgcolor="#ebf9b4" >
	
	<tr>
		<td align="left" colspan="6"><b>Student List</b></td>
    </tr>
        <tr>
			<td class="label" align="center"><strong>No.</strong></td>
            <td class="label" align="center"><strong>Student Name</strong></td>
           <td class="label" align="center"><strong>Registered Date</strong></td>
	
	';
		$section = mysql_query("SELECT * FROM tblUser WHERE user_type = 'student' AND forumaccess = 0 AND status = 'no'");
		$noOfSection = mysql_num_rows($section);
		$loop = 1;
		$ctr = 1;
		
		while($ctr <= $noOfSection){
		
		while($row_section = mysql_fetch_assoc($section)):
			$u = $row_section['userid'];
			date_default_timezone_set('Asia/Manila');
			$date = date('F j, Y, g:i a', strtotime($row_section['datejoin']));
		
		$a++;
		if ($a%2==1)
		{ 
		echo "<tr bgcolor='#e6e4b1'>";
		}
		else
		{
			echo "<tr bgcolor='#c9e362'>";	
		}
		
		print '
						<td align="left">'.$loop.'.</td>
						<td align="center" class="extra"><a title="Edit '.$row_section['lname'].'" href="regStudAdviser.php?userid='.$row_section['userid'].'">'.$row_section['fname'].' '.$row_section['mname'].' '.$row_section['lname'].'</a></td>
						<td align="center" class="extra">'.$date.'</td>
					
						';	
		$loop++;
		endwhile;
		$ctr++;
		}
 print '</table>';
		
}

//view teacher

public function view_teachers(){

		print '
	<table width="680" align="center" bordercolor="#000000" " bgcolor="#ebf9b4" >
	<tr>
		<td align="left" colspan="6"><b>Enrolled List</b></td>
    </tr>
        <tr>
			<td class="label" align="center"><strong>No.</strong></td>
            <td class="label" align="center"><strong>Student Name</strong></td>
            <td class="label" align="center"><strong>Section</strong></td>
           <td class="label" align="center"><strong>Date and Time</strong></td>
	
	';
		$section = mysql_query("SELECT * FROM tblUser u WHERE user_type = 'teacher' ");
		$noOfSection = mysql_num_rows($section);
		$loop = 1;
		$ctr = 1;
		
		while($ctr <= $noOfSection){
		
		while($row_section = mysql_fetch_assoc($section)):
			$u = $row_section['userid'];
			date_default_timezone_set('Asia/Manila');
			$date = date('F j, Y, g:i a', strtotime($row_section['datejoin']));
		
		$a++;
		if ($a%2==1)
		{ 
		echo "<tr bgcolor='#e6e4b1'>";
		}
		else
		{
			echo "<tr bgcolor='#c9e362'>";	
		}
		
		print '
						<td align="left">'.$loop.'.</td>
						<td align="center" class="extra"><a href="viewUser2.php?id='.$u.'">'.$row_section['fname'].' '.$row_section['mname'].' '.$row_section['lname'].'</a></td>
						<td align="center" class="extra">'.$row_section['sectionName'].'</td>
						<td align="center" class="extra">'.$date.'</td>
					
						';	
		$loop++;
		endwhile;
		$ctr++;
		}
 print '</table>';
	}

//for Login
public function check_login($emailusername, $password, $user_type)
 
{
//$password = md5($password);
//$userArray = array('admin','customer');
$ql = "SELECT userid,user_type FROM tblUser WHERE email = '$emailusername' 
						OR username ='$emailusername' AND password = '$password'";
						
$result = mysql_query($ql) or die(mysql_error());
						

$user_data = mysql_fetch_array($result);

$no_rows = mysql_num_rows($result);
if ($no_rows == 1) 
{
$_SESSION['login'] = true;
$_SESSION['userid'] = $user_data['userid'];
$_SESSION['user_type'] = $user_data['user_type'];
$_SESSION['username'] = $user_data['username'];

//for testing print_r($user_data);

return TRUE;
}
else
{
$_SESSION['login'] = false;
$_SESSION['userid'] = "";
$_SESSION['user_type'] = "";

return FALSE;
}
}
// Getting session 
public function get_session() 
{
return $_SESSION['login'];
}
//for logout
public function user_logout()
	{
		$_SESSION['login'] = FALSE;
			$u = $_SESSION['curuser'];
				date_default_timezone_set('Asia/Manila');
				$date = date("l jS \of F Y h:i:s A");
				
				mysql_query("UPDATE tbluser SET lastlogin = '$date' WHERE userid = '$u'");
		session_destroy();
	}
public function test()
{	
echo 'hello';
}

public function validSession(){
		
		if (!isset($_SESSION['userid']))
{
	$strUser="";
}
else
{
	$strUser=$_SESSION['userid'];
}
	
	if ($strUser== ""):
	print "<HTML><HEAD><TITLE> Session Expired </TITLE>";
	print "<SCRIPT LANGUAGE=\"JAVASCRIPT\">\n" ;
/*	print "<link rel='shortcut icon' href='../assets/favicon.ico' />";*/
	print "<!--\n" ;
	print "function onLoad1()\n";
	print "{\n\t";
	print  "alert('Your session is expired. Please Login again!')\n\t" ;
	
	print  "setTimeout(\"parent.location='" . $strRootpath . "../view/login_r.php'\",1000)\n" ;
	print "}";
	print "// -->\n" ;
	print "</SCRIPT>\n</HEAD><BODY onLoad=\"JavaScript:onLoad1();\"></BODY></HTML>";
endif;

	}	
	
public function ViewStudents() {
	
	print "<HTML><HEAD><TITLE></TITLE>\n";
	print "<SCRIPT LANGUAGE=\"JAVASCRIPT\">\n";
	print "<!--\n" ;
	print "function onLoad1()\n";	
	print "{\t\n";
	print  "setTimeout(\"parent.location='../view/viewStudents.php'\",10)\n";
	
	print "}\n";
	print "// -->\n";
	print "</SCRIPT>\n";

	print "</HEAD>\n<BODY onLoad=\"JavaScript:onLoad1();\">\n";


}

public function notValue() {
	
	$_SESSION["userid"]="";
	$_SESSION["email"]="";

	session_destroy();
	print "<HTML><HEAD><TITLE>Logged out </TITLE>\n";
	print "<SCRIPT LANGUAGE=\"JAVASCRIPT\">\n";
	print "<!--\n" ;
	print "function onLoad1()\n";	
	print "{\t\n";
	print  "setTimeout(\"parent.location='../view/index.php'\",10)\n";
	
	print "}\n";
	print "// -->\n";
	print "</SCRIPT>\n";

	print "</HEAD>\n<BODY onLoad=\"JavaScript:onLoad1();\">\n";


}

public function view_sectionStudents(){
	print '
	<table width="650" align="center" bordercolor="#000000" " bgcolor="#ebf9b4" >
	<tr>
		<td align="left" colspan="6">Enrolled List</td>
    </tr>
        <tr>
			<td class="label" align="center"><strong>No.</strong></td>
            <td class="label" align="center"><strong>Student Name</strong></td>
            <td class="label" align="center"><strong>Section</strong></td>
           <td class="label" align="center"><strong>Date and Time</strong></td>
	
	';	$sec = mysql_query("SELECT * from tblsection WHERE studentNumber = '$_SESSION[curuser]'");
		while($rowSec = mysql_fetch_assoc($sec)){
		$secName = $rowSec['sectionName'];	
		}
		$section = mysql_query("SELECT * FROM tblsection WHERE sectionName = '$secName'  AND studentName != 'none'");
		$noOfSection = mysql_num_rows($section);
		$loop = 1;
		$ctr = 1;
		
		while($ctr <= $noOfSection){
		
		while($row_section = mysql_fetch_assoc($section)):
			$u = $row_section['studentNumber'];
			date_default_timezone_set('Asia/Manila');
			$date = date('F j, Y, g:i a', strtotime($row_section['datejoin']));
		print '
					<tr>
						<td align="left">'.$loop.'.</td>
						<td align="center" class="extra"><a href="viewUsers.php?id='.$u.'">'.$row_section['studentName'].'</a></td>
						<td align="center" class="extra">'.$row_section['sectionName'].'</td>
						<td align="center" class="extra">'.$date.'</td>
					
						';	
		$loop++;
		endwhile;
		$ctr++;
		}
 print '</table>';
}
}
//class close


?>